<?php
require_once 'Connection.php';

/**
 * class PHONGBAN
 *
 * MaPhong, TenPhong
 *
 */
class PHONGBAN {
	/**
	 * Create
	 *
	 * @return bool 
	 * Auto set Id field
	 */
	public static function Create(&$obj) {
		$con = Connection::GetConnection();
		$query = sprintf("INSERT INTO PHONGBAN(TenPhong) VALUES ('%s')",
					mysql_real_escape_string($obj['TenPhong'])
				);
		$result = mysql_query($query, $con);
		$obj['MaPhong'] = mysql_insert_id($con);
		return $result;
	}
	
	/**
	 * Update
	 *
	 * @return bool 
	 */
	public static function Update($obj) {
		$con = Connection::GetConnection();
		$query = sprintf("UPDATE PHONGBAN SET TenPhong = '%s' WHERE MaPhong = %d",
					mysql_real_escape_string($obj['TenPhong']),
					$obj['MaPhong']
				);
		$result = mysql_query($query, $con);
		return $result;
	}
	
	/**
	 * Read
	 *
	 * @return fields
	 */
	public static function Read($maPhong) {
		$con = Connection::GetConnection();
		$query = sprintf("SELECT MaPhong, TenPhong FROM PHONGBAN WHERE MaPhong = %d", $maPhong);
		$lst = mysql_query($query, $con);
		if (mysql_num_rows($lst) != 1) return null;
		$item = mysql_fetch_assoc($lst);
		return $item;
	}
	
	/**
	 * Delete
	 *
	 * @return bool
	 */
	public static function Delete($obj) {
		$con = Connection::GetConnection();
		$query = sprintf("DELETE FROM PHONGBAN WHERE MaPhong = %d", $obj['MaPhong']);
		$result = mysql_query($query, $con);
		return $result;
	}
	
	/**
	 * Get all department
	 *
	 * @return array of fields
	 */
	public static function Select() {
		$con = Connection::GetConnection();
		$query = "SELECT MaPhong, TenPhong FROM PHONGBAN";
		$lst = mysql_query($query, $con);
		$result = array();
		while ($item = mysql_fetch_assoc($lst))
			array_push($result, $item);
		return $result;
	}
	
	/**
	 * Get number of employee in department
	 *
	 * @return int
	 */
	public static function GetEmployeeCount($maPhong = -1) {
		$con = Connection::GetConnection();
		$query = "SELECT COUNT(*) AS SoNV FROM NHANVIEN";
		if ($maPhong != -1)
			$query .= " WHERE Phong = $maPhong";
		
		$res = mysql_query($query, $con);
		$item = mysql_fetch_assoc($res);
		return $item['SoNV'];
	}
	
	/**
	 * Validate input
	 *
	 * @return array of error
	 */
	public static function Validate($input) {
		$errors = array();
		PHONGBAN::CheckTenPhong($input, $errors);
		return $errors;
	}
	
	// TenPhong: required, maxlen(50), unique
	private static function CheckTenPhong($input, &$errors) {
		try {
			// required
			if ($input['TenPhong'] == '') throw new Exception('Chưa nhập tên phòng');
			// maxlen(50)
			if (strlen($input['TenPhong']) > 50) throw new Exception('Tên phòng phải ngắn hơn 50 ký tự');
			// unique			
			$depts = PHONGBAN::Select();
			foreach ($depts as $dept) {
				if ($dept['TenPhong'] == $input['TenPhong']) 
					throw new Exception('Tên phòng ban bị trùng');
			}
		} catch (Exception $e) {
			$errors['TenPhong'] = $e->getMessage();		
		}
	}
}
?>